<?php
App::uses('AppHelper', 'View/Helper');
//App::import('Component', 'Utilities');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       lib.View.Helper
 * @author Duonght <hduongdmit@gmail.com>
 */
class UtilitiesHelper extends AppHelper {

	/**
	 * Create internal link
	 *
	 * @param string $type
	 * @param unknown $url
	 * @param number $rewrite
	 * @return string
	 */
	public function createLink($type = "detail", $url = array(), $rewrite = 1){
		$menuReturn = '';
		$keyReplace = '/';
		if($rewrite == 0){
			$menuReturn = $keyReplace . $type . ".php?";
			foreach($url as $key=>$value){
				if($key == "module") $value = strtolower($value);
				if($key != "title"){
					$menuReturn .= $key . "=" . $value . "&";
				}
			}

			$menuReturn = substr($menuReturn,0, strrpos($menuReturn, "&"));
			//tra ve url ko rewrite
			return $menuReturn;
		}

		if(isset($url["module"])){
			switch($url["module"]){
				case 'news':
					$module = "n";
					break;
				case 'static':
					$module = 's';
					break;
			}
		}

		switch($type){
			case "type":
				$pcat = '';
				$menuReturn = ((isset($url["Pcat"])&&!empty($url["Pcat"]))?'/' . $url["Pcat"]:'') . '/' . $url["Rcat"] . (isset($module)?'-'.$module:'') .'.html';
				break;
			case "detail":
				$title = strtolower($this->removeTitle($url["title"], $keyReplace));
				$menuReturn = ((isset($url["Pcat"])&&!empty($url["Pcat"]))?'/' . $url["Pcat"]:'') . '/' . $url["Rcat"] . '/' . $title .'.html';
				break;
		}

		return $menuReturn;
	}

	/**
	 *
	 * @param string $string
	 * @param string $keyReplace
	 * @return mixed
	 */
	public function removeTitle($string, $keyReplace = '/'){
		$string = self::unicodeConvert($string);
		// neu muon de co dau
		// $string = trim(preg_replace("/[^A-Za-z0-9àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]/i"," ",$string));

		$string = trim(preg_replace("/[^A-Za-z0-9]/i"," ",$string)); // khong dau
		$string = str_replace(" ","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace("--","-",$string);
		$string = str_replace($keyReplace, "-", $string);

		return strtolower($string);

	}

	/**
	 *
	 * @param string $str
	 * @return mixed
	 */
	public static function unicodeConvert($str){
		$vietnamese = array(
				"à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ",
				"è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à"
		);

		$unicode = array(
				"a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a"
		);

		return str_replace($vietnamese, $unicode, $str);
	}

	/**
	 * loại bỏ hoạt động của các thẻ html, vô hiệu hóa
	 *
	 * @param string $str
	 * @return mixed
	 */
	public function htmlspecialbo($str = ''){
		$arrDenied = array('<', '>', '"');
		$arrReplace = array('&lt;', '&gt;', '&quot;');
		$str = str_replace($arrDenied, $arrReplace, $str);
		return $str;
	}

	/**
	 * loại bỏ các thẻ html, javascript
	 *
	 * @param string $string
	 * @return mixed
	 */
	public function removeHTML($string = ''){
		$string = mb_convert_encoding($string, "UTF-8", "UTF-8");
		$string = preg_replace ('/<script.*?\>.*?<\/script>/si', ' ', $string);
		$string = preg_replace ('/<style.*?\>.*?<\/style>/si', ' ', $string);
		$string = preg_replace ('/<.*?\>/si', ' ', $string);
		$string = str_replace ('&nbsp;', ' ', $string);
		//$string = html_entity_decode ($string);
		return $string;
	}

}

